<?php

class ConfAction extends CommonAction
{
    public function index()
    {
        $conf_res = M("Conf")->where("is_effect = 1 and is_conf = 1")->order("group_id asc,sort asc")->select();
        foreach ($conf_res as $k => $v) {
            $v['value'] = htmlspecialchars($v['value']);
            if ($v['name'] == 'TEMPLATE') {

                //输出现有模板文件夹
                $directory = SITE_PATH . "application/home/Tpl/";
                $dir = @opendir($directory);
                $tmpls = array();

                while (false !== ($file = @readdir($dir))) {
                    if ($file != '.' && $file != '..')
                        $tmpls[] = $file;
                }
                @closedir($dir);
                //end

                $v['input_type'] = 1;
                $v['value_scope'] = $tmpls;
            } elseif ($v['name'] == 'SHOP_LANG') {
                //输出现有语言包文件夹
                $directory = SITE_PATH . "application/home/Lang/";
                $dir = @opendir($directory);
                $tmpls = array();

                while (false !== ($file = @readdir($dir))) {
                    if ($file != '.' && $file != '..')
                        $tmpls[] = $file;
                }
                @closedir($dir);
                //end

                $v['input_type'] = 1;
                $v['value_scope'] = $tmpls;
            } else
                $v['value_scope'] = explode(",", $v['value_scope']);
            $conf[$v['group_id']][] = $v;
        }
        $this->assign("conf", $conf);
        $this->display();
    }

    public function update()
    {
        $conf_res = M("Conf")->where(array("is_effect" => "1", "is_conf" => "1"))->select();
        foreach ($conf_res as $k => $v) {
            conf($v['name'], $_REQUEST[$v['name']]);
            if ($v['name'] == 'URL_MODEL' && $v['value'] != $_REQUEST[$v['name']]) {
                clear_auto_cache("byouhui_filter_nav_cache");
                clear_auto_cache("cache_shop_acate_tree");
                clear_auto_cache("cache_shop_cate_tree");
                clear_auto_cache("cache_youhui_cate_tree");
                clear_auto_cache("city_list_result");
                clear_auto_cache("fyouhui_filter_nav_cache");
                clear_auto_cache("get_help_cache");
                clear_auto_cache("page_image");
                clear_auto_cache("tuan_filter_nav_cache");
                clear_auto_cache("youhui_page_recommend_youhui_list");
                clear_auto_cache("ytuan_filter_nav_cache");
                clear_auto_cache("store_filter_nav_cache");
                clear_dir_file(SITE_PATH . "system/runtime/app/data_caches/");
                clear_dir_file(SITE_PATH . "system/runtime/app/tpl_caches/");
                clear_dir_file(SITE_PATH . "system/runtime/app/tpl_compiled/");

                clear_dir_file(SITE_PATH . "system/runtime/app/data_caches/");
                clear_dir_file(SITE_PATH . "system/runtime/data/page_static_cache/");
                clear_dir_file(SITE_PATH . "system/runtime/data/dynamic_avatar_cache/");
            }
        }

        //开始写入配置文件
        $sys_configs = M("Conf")->select();
        $config_replace = array();
        foreach ($sys_configs as $k => $v) {
            $config_replace[$v['name']] = addslashes($v['value']);
        }
        $filename = SITE_PATH . "system/config.php";
        $config = include($filename);

        if (!$handle = fopen($filename, 'w')) {
            $this->error(l("OPEN_FILE_ERROR") . $filename);
        }
        $config = array_replace_recursive($config, $config_replace);
        $config_content = sprintf('<?php return %s;?>', var_export($config, true));

        if (fwrite($handle, $config_content) === FALSE) {
            $this->error(l("WRITE_FILE_ERROR") . $filename);
        }
        unset($config_replace);
        unset($config_content);

        fclose($handle);
        save_log(l("CONF_UPDATED"), 1);
        $this->success(L("UPDATE_SUCCESS"));
    }


}

?>